package com.lixinxin.benbenlove.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.lixinxin.benbenlove.db.dao.NoteDao
import com.lixinxin.benbenlove.db.data.NoteData

@Database(entities = [NoteData::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun noteDao(): NoteDao

    companion object {

        private var instance: AppDatabase? = null

        private fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                Room.databaseBuilder(context, AppDatabase::class.java, "db_ben_ben_love.db")
                    .fallbackToDestructiveMigration()
                    .build().also { instance = it }
            }
        }

        fun getNoteDao(context: Context): NoteDao {
            return getInstance(context).noteDao()
        }

    }
}